package com.atguigu.yygh.hosp.testmongo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/mongo2")
public class TestMongo2 {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("create")
    public void createUser() {

        User user = new User();
        user.setAge(19);
        user.setName("tom");
        user.setEmail("tom@163.com");

        userRepository.save(user);
        System.out.println("user = " + user);
    }
    //查询所有
    @GetMapping("findAll")
    public void findUser() {
        for (User user : userRepository.findAll()) {
            System.out.println("user = " + user);
        }

    }

    //id查询
    @GetMapping("findId")
    public void getById() {
        User user = userRepository.findById("63fc8fc55ba85c2e8ed77be2").get();
        System.out.println("user = " + user);
    }


    //条件查询
    @GetMapping("findQuery")
    public void findUserList() {
        User user = new User();
        user.setAge(19);
        user.setName("tom");
        Example<User> example = Example.of(user);
        List<User> userList = userRepository.findAll(example);
        userList.forEach(System.out::println);

    }
    @GetMapping("findLike")
    public void findUsersLikeName() {
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        User user = new User();
        user.setName("om");
        Example example = Example.of(user, matcher);
        List<User> userList = userRepository.findAll(example);
        userList.forEach(System.out::println);
    }


    @GetMapping("findPage")
    public void findUsersPage() {

        Sort sort = Sort.by(Sort.Direction.DESC, "age");
        Pageable pageable = PageRequest.of(0, 10, sort);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        User user = new User();
        user.setName("om");
        Example<User> example = Example.of(user, matcher);
        Page<User> userPage = userRepository.findAll(example, pageable);
        System.out.println("userPage = " + userPage);

    }

    @GetMapping("update")
    public void updateUser() {
        User user = userRepository.findById("63fc8fc55ba85c2e8ed77be2").get();
        user.setEmail("123@163.com");
        User user1 = userRepository.save(user);
        System.out.println("user1 = " + user1);
    }

    //删除
    @GetMapping("delete")
    public void delete() {
        userRepository.deleteById("63fc8fc55ba85c2e8ed77be2");
    }

    @GetMapping("testMethod1")
    public void testMethod1() {
        List<User> userList = userRepository.findByNameLike("os");
        userList.forEach(System.out::println);
    }

    @GetMapping("testMethod2")
    public void testMethod2() {
        List<User> jack = userRepository.findByNameAndAge("jack", 22);
        jack.forEach(System.out::println);
    }
    @GetMapping("testMethod3")
    public void testMethod3() {

    }
}
